<?php
/*
File: stats_dispatch.php
Version: 2.0
Date created: 
Last Edited : 26-07-2008
Author: Craig G Smith<craig@joomla-host.co.za
*/

  defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }
  $action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();

// Below is the job to convert both dates in desired timestamp
if(isset($_REQUEST['start_date']))
{
	$start_date = $_REQUEST['start_date'];
	$st_timestamp = explode('/',$start_date);
	$start_date_timestamp = $st_timestamp[2].'-'.$st_timestamp[1].'-'.$st_timestamp[0].' 00:00:00';
}
else
{
	// Initially I have to show all records so have mentioned the start date as 01/01/1900 
	$start_date=  date("01/01/1900");
	$st_timestamp = explode('/',$start_date);
	$start_date_timestamp = $st_timestamp[2].'-'.$st_timestamp[1].'-'.$st_timestamp[0].' 00:00:00';
}	
if(isset($_REQUEST['end_date']))
{	$end_date = $_REQUEST['end_date'];
	$ed_timestamp = explode('/',$end_date);
	$end_date_timestamp = date($ed_timestamp[2].'-'.$ed_timestamp[1].'-'.$ed_timestamp[0].' 23:59:59');
}
else
{
	$end_date_timestamp= gmdate("Y-m-d H:i:s", time() + 3600*5.5);
	list($year, $month, $date, $hour, $minute, $second) = split('[- :]', $end_date_timestamp);
	$ed_timestamp = explode('/',$end_date);
	$end_date = $date.'/'.$month.'/'.$year;
}	

//echo $start_date_timestamp;
//echo $end_date_timestamp;

$status = (isset($_REQUEST['status']) ? $_REQUEST['status'] : '3');
$vendorid = (isset($_REQUEST['vendorid']) ? $_REQUEST['vendorid'] : '');
$vendor_query = '';
if ($vendorid != '') {
$vendor_query = " and c.vendors_id = '$vendorid' ";
}


  $dispatch_query = tep_db_query("SELECT a.date_added as date, a.orders_id as order_id, b.customers_name as customer_name, b.delivery_name, b.delivery_city, b.delivery_state, b.delivery_postcode, c.products_name as product, c.products_quantity as quantity, c.final_price as price,  c.products_quantity * c.final_price as total_price FROM " . TABLE_ORDERS_STATUS_HISTORY . " a, " . TABLE_ORDERS . " b, " . TABLE_ORDERS_PRODUCTS . " c 
WHERE a.orders_id=b.orders_id $vendor_query AND b.orders_id=c.orders_id AND a.orders_status_id=".$status." AND a.date_added BETWEEN '".$start_date_timestamp."' AND '".$end_date_timestamp."'");

?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">
<script language="javascript" src="<?php echo DIR_WS_COMPONENT ?>includes/general.js"></script>
<script type="text/javascript" src="<?php echo DIR_WS_COMPONENT ?>calendar/javascripts/calendardisplay.js"></script>
<script type="text/javascript" src="<?php echo DIR_WS_COMPONENT ?>calendar/javascripts/calendar.js"></script>
<script type="text/javascript">
window.addEvent('domready', function(){
	$$('input.DatePicker').each( function(el){
		new DatePicker(el);
		});
	});
</script>
<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>calendar/stylesheets/calendar.css" media="all">


<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
        </table></td>
<!-- body_text //-->
    <td width="100%" valign="top">
    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td>	
		<table border="0" width="75%" cellspacing="0" cellpadding="0">
		<form action="<?php echo tep_href_link("stats_dispatch.php") ?>" method="POST">
		<tr>
		<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
		</tr>
		<tr>
		<td class="main"><br><label for="<?php echo ENTRY_START_DATE ?>"><?php echo ENTRY_START_DATE ?></label><?php echo tep_draw_input_field('start_date', $start_date, 'size="20", id="start_date", class="DatePicker"') ?> &nbsp;&nbsp;
        <label for="<?php echo ENTRY_END_DATE ?>"><?php echo ENTRY_END_DATE ?></label><?php echo tep_draw_input_field('end_date', $end_date, 'size="20", id="end_date", class="DatePicker"') ?> Order Status : <select name="status">
        <?php
        $orders_status_query = tep_db_query("select orders_status_name, orders_status_id from " . TABLE_ORDERS_STATUS . " where language_id = '" . $languages_id . "'");
  while ($orders_status = tep_db_fetch_array($orders_status_query)) {
  echo '<option value="'.$orders_status['orders_status_id'].'" ';
  if ($orders_status['orders_status_id'] == $status) {
  echo ' SELECTED ';
  }
  echo '>' . $orders_status['orders_status_name'] . '</option>';
  
  
  }
  ?></select> 
  
   || By Vendor : <select name="vendorid">
   <option value="">All</option>
   <?php
   $vendors_query = tep_db_query("select vendors_id, vendors_name from " . TABLE_VENDORS );
 // $vendors_query = tep_db_query("select vendors_id, vendors_name from `" . TABLE_VEN DORS . "`");
  while ($vendors_status = tep_db_fetch_array($vendors_query)) {
  echo '<option value="'.$vendors_status['vendors_id'].'" ';
  if ($vendors_status['vendors_id'] == $vendorid) {
  echo ' SELECTED ';
  }
  echo '>' . $vendors_status['vendors_name'] . '</option>';
  
  
  }
   
   
   ?></select> 
   
       <?php echo tep_image_submit('button_generate_report.gif', 'Generate'); ?>
		</form>
		</table></td></tr>

      <tr>
        <td><br></td>
      </tr>

      <tr>
        <td>

<?php  if(tep_db_num_rows($dispatch_query) > 0) { ?>
	
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="1" cellpadding="1" class="adminlist"><thead>
              <tr class="dataTableHeadingRow">
			    <th class="dataTableHeadingContent" width="19%"><?php echo TABLE_HEADING_DATE; ?></th>
                <th class="dataTableHeadingContent" width="9%"><?php echo TABLE_HEADING_ORDER_ID; ?></td>
                <th class="dataTableHeadingContent" align="center" width="30%"><?php echo TABLE_HEADING_PRODUCT; ?></td>
                <th class="dataTableHeadingContent" align="center" width="15%"><?php echo TABLE_HEADING_CUSTOMER; ?></td>
                <th class="dataTableHeadingContent" align="center" width="15%"><?php echo TABLE_HEADING_DELIVERY; ?></td>
                <th class="dataTableHeadingContent" align="center" width="15%"><?php echo TABLE_HEADING_PRICE; ?></td>
                <th class="dataTableHeadingContent" align="center" width="15%"><?php echo TABLE_HEADING_QUANTITY; ?></td>
				<th class="dataTableHeadingContent" align="center" width="15%"><?php echo TABLE_HEADING_AMOUNT; ?>&nbsp;</td>
              </tr></thead>
 <?php while ($dispatch = tep_db_fetch_array($dispatch_query)) { ?>
              <tr class="dataTableRow">
			    <td class="dataTableContent"><?php echo date("d-m-Y H:i:s",strtotime($dispatch ['date'])); ?></td>
                <td class="dataTableContent"><?php echo $dispatch ['order_id']; ?></td>
                <td class="dataTableContent"><?php echo $dispatch ['product']; ?></td>
                <td class="dataTableContent" align=center><?php echo $dispatch ['customer_name']; ?></td>
                <td class="dataTableContent" align=center><?php echo $dispatch ['delivery_name'].' '.$dispatch ['delivery_city'].', '.$dispatch ['delivery_state'].' - '.$dispatch ['delivery_postcode']; ?></td>
                <td class="dataTableContent" align=center><?php echo $dispatch ['price']; ?></td>
                <td class="dataTableContent" align=center><?php echo $dispatch ['quantity']; ?></td>
				<td class="dataTableContent" align=right><?php echo $currencies->display_price($dispatch ['total_price'],0); ?>&nbsp;</td>
              </tr>

<?php } ?>
<?php
    echo '<tr><td colspan=8>'.tep_draw_separator('pixel_trans.gif', '100%', '10').'</td></tr>';
?>
    
          </table>
	<?php } else { ?>
		<hr color="#A3D65E">
		<font color="#FF0000" face="Verdana, Arial" size="2"><strong>No Record Found !!</strong></font><br><hr color="#A3D65E">
	<?php } ?>
		  </td>
          </tr>
        </table></td>

 <!-- body_text_eof //-->
</tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
